<template>
  <div>
    <zh-card title="">
      <zh-form :model="pageData.queryParams" ref="queryFormRef">
        <zh-form-input prop="statementId" label="动态语句主键" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.statementId"></zh-form-input>
        <zh-form-input prop="statementName" label="语句名称" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.statementName"></zh-form-input>
        <zh-form-input prop="statementDescription" label="语句描述" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.statementDescription"></zh-form-input>
        <zh-form-input prop="statement" label="语句" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.statement"></zh-form-input>
        <zh-form-select prop="statementType" label="语句类型 1-sql 2-java" allowClear :span="8"
                        :options="pageData.dictData.STATEMENTTYPE"
                        v-model="pageData.queryParams.statementType"></zh-form-select>
        <zh-form-input prop="interfaceId" label="动态接口主键" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.interfaceId"></zh-form-input>
        <zh-form-input prop="orderNo" label="排序号" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.orderNo"></zh-form-input>
        <zh-form-input prop="callInterfaceId" label="调用动态接口主键" type="text" :span="8" dataType="common"
                       v-model="pageData.queryParams.callInterfaceId"></zh-form-input>
        <zh-form-query @queryClick="queryDynamicStatement" :formRef="queryFormRef" :show-advanced="false">
          <el-button @click="showAddDialog">添加</el-button>
        </zh-form-query>
      </zh-form>
    </zh-card>
    <zh-table title="动态语句列表" ref="dynamicStatementTable"
              size="mini"
              :funcCode="'dynamicStatement0001'"
              :columns="pageData.tableColumns"
              highlight-current-row>
      <template #op="scope">
        <el-tooltip content="编辑" placement="top">
          <el-button :icon="Edit" link @click="showEditDialog(scope)"></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button :icon="Delete" type="danger" link @click="delDynamicStatement(scope.row)"></el-button>
        </el-tooltip>
        <el-tooltip content="详情" placement="top">
          <el-button :icon="Document" link @click="showDetail(scope)"></el-button>
        </el-tooltip>
      </template>
    </zh-table>
    <el-dialog title="添加动态语句" v-model="pageData.addDynamicStatementDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="statementId" label="动态语句主键" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.statementId"></zh-form-input>
        <zh-form-input prop="statementName" label="语句名称" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.statementName"></zh-form-input>
        <zh-form-input prop="statementDescription" label="语句描述" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.statementDescription"></zh-form-input>
        <zh-form-input prop="statement" label="语句" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.statement"></zh-form-input>
        <zh-form-select prop="statementType" label="语句类型 1-sql 2-java" allowClear :span="8"
                        :options="pageData.dictData.STATEMENTTYPE"
                        v-model="pageData.formData.statementType"></zh-form-select>
        <zh-form-input prop="interfaceId" label="动态接口主键" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.interfaceId"></zh-form-input>
        <zh-form-input prop="orderNo" label="排序号" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.orderNo"></zh-form-input>
        <zh-form-input prop="callInterfaceId" label="调用动态接口主键" type="text" :span="8" dataType="common"
                       v-model="pageData.formData.callInterfaceId"></zh-form-input>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">

          <el-button @click="pageData.addDynamicStatementDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addDynamicStatement">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="修改动态语句" v-model="pageData.editDynamicStatementDialogVisible">
      <zh-form :rules="pageData.formRules">
        <zh-form-input prop="statementId" label="动态语句主键"
                       v-model="pageData.editFormData.statementId"></zh-form-input>
        <zh-form-input prop="statementName" label="语句名称"
                       v-model="pageData.editFormData.statementName"></zh-form-input>
        <zh-form-input prop="statementDescription" label="语句描述"
                       v-model="pageData.editFormData.statementDescription"></zh-form-input>
        <zh-form-select prop="statementType" label="语句类型 1-sql 2-java" allowClear
                        :options="pageData.dictData.STATEMENTTYPE"
                        v-model="pageData.editFormData.statementType"></zh-form-select>
        <zh-form-input prop="interfaceId" label="动态接口主键"
                       v-model="pageData.editFormData.interfaceId"></zh-form-input>
        <zh-form-input prop="orderNo" label="排序号"
                       v-model="pageData.editFormData.orderNo"></zh-form-input>
        <zh-form-input prop="callInterfaceId" label="调用动态接口主键"
                       v-model="pageData.editFormData.callInterfaceId"></zh-form-input>
        <zh-form-input prop="statement" label="语句" type="textarea" :rows="4" :span="24"
                       v-model="pageData.editFormData.statement"></zh-form-input>
      </zh-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="pageData.editDynamicStatementDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editDynamicStatement">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup name="dynamicStatement">
import {Delete, Edit, Document} from '@element-plus/icons-vue'
import {ElMessageBox} from "element-plus";
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  init();
})

// ------------------定义子组件--------------------------
const dynamicStatementTable = ref();
const queryFormRef = ref();
// ------------------变量区------------------------------
const pageData = reactive({
  addDynamicStatementDialogVisible: false,
  editDynamicStatementDialogVisible: false,
  detailDialog: false,
  tableColumns: [
    {type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号'},
    {field: 'statementId', title: '动态语句主键', visible: false, align: 'center', width: 100},
    {field: 'statementName', title: '语句名称', visible: true, align: 'center', width: 100},
    {field: 'statementDescription', title: '语句描述', visible: true, align: 'center', width: 200},
    {field: 'statement', title: '语句', visible: true, align: 'center', width: 200},
    {field: 'statementType', title: '语句类型 1-sql 2-java', visible: true, align: 'center', width: 100},
    {field: 'interfaceId', title: '动态接口主键', visible: true, align: 'center', width: 100},
    {field: 'orderNo', title: '排序号', visible: true, align: 'center', width: 100},
    {field: 'callInterfaceId', title: '调用动态接口主键', visible: true, align: 'center', width: 100},
    {field: 'op', title: '操作', visible: true, align: 'center', width: 100, fixed: 'right', slots: {default: 'op'}},
  ],
  queryParams: {
    statementId: '',
    statementName: '',
    statementDescription: '',
    statement: '',
    statementType: '',
    interfaceId: '',
    orderNo: '',
    callInterfaceId: ''
  },
  formData: {
    statementId: '',
    statementName: '',
    statementDescription: '',
    statement: '',
    statementType: '',
    interfaceId: '',
    orderNo: '',
    callInterfaceId: ''
  },
  editFormData: {
    statementId: '',
    statementName: '',
    statementDescription: '',
    statement: '',
    statementType: '',
    interfaceId: '',
    orderNo: '',
    callInterfaceId: ''
  },
  formRules: {},
  dictFields: 'STATEMENTTYPE',
  dictData: [],
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  queryDynamicStatement();
}
// 查询数据字典信息
const getDict = () => {
  let params = {
    dictFields: pageData.dictFields,
  };
  $api.common.getBatchDict.post(params).then((res: any) => {
    pageData.dictData = res.result;
  });
};
const queryDynamicStatement = () => {
  dynamicStatementTable.value.query(pageData.queryParams);
}
const showAddDialog = () => {
  pageData.addDynamicStatementDialogVisible = true;
  pageData.formData = {
    statementId: '',
    statementName: '',
    statementDescription: '',
    statement: '',
    statementType: '',
    interfaceId: '',
    orderNo: '',
    callInterfaceId: ''
  }
}
const showEditDialog = (scope) => {
  pageData.editDynamicStatementDialogVisible = true;
  pageData.editFormData = {...scope.row}
}
const addDynamicStatement = () => {
  $api.common.send.post('dynamicStatement0004', pageData.formData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('添加动态语句成功');
      pageData.addDynamicStatementDialogVisible = false;
      queryDynamicStatement();
    }
  })
}
const editDynamicStatement = () => {
  $api.common.send.post('dynamicStatement0005', pageData.editFormData).then(res => {
    if (res.code == 0) {
      ElMessageBox.alert('修改动态语句成功');
      pageData.editDynamicStatementDialogVisible = false;
      queryDynamicStatement();
    }
  })
}
const delDynamicStatement = ({statement_id}) => {
  ElMessageBox.confirm('确定删除该动态语句?').then(() => {
    $api.common.send.post('dynamicStatement0006', {statement_id: statement_id}).then(res => {
      if (res.code == 0) {
        ElMessageBox.alert('删除动态语句成功');
        queryDynamicStatement();
      }
    })
  })
}

</script>
<style lang="scss" scoped></style>
